/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dbsucxentW.storageW.parserW;

/**
 *
 * @author truongbaquan
 */
public class AncestorDeweyGroupCalculator {
    private DocumentSummary summary;
    private long[] sum;

    public AncestorDeweyGroupCalculator(DocumentSummary summary) {
        this.summary = summary;
        this.sum = new long[summary.getMaxDepth() + 1];
        for (int i = 0; i < sum.length; i++) {
            sum[i] = 0;
        }
    }
    
    /**
     * Calculate AncestorDeweyGroup
     * @param branchOrder   The new node's BranchOrder
     * @param modified      A flag indicating whether the calculation should update the internal variable.
     * If false, the next calculation would be the same.
     * @return
     */
    public long calculate(int branchOrder, boolean modified) {
        if (branchOrder > 0) {
            long ancestorDeweyGroup = sum[branchOrder] + summary.getAncestorValue(branchOrder);
            if (modified) {
                int q;
                for (q = branchOrder; q <= summary.getMaxDepth(); q++) {
                    sum[q] = ancestorDeweyGroup;
                }
            }
            return ancestorDeweyGroup;
        }
        else return 0;
    }
}
